using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;

namespace RBSViewer
{
    public partial class LookupTablePicker : Form
    {
        public Guid CustomFieldGuid;
        public Guid LookupTableGuid;
        private const string CUSTOMFIELDSSWEBSERVICE = "_vti_bin/PSI/CustomFields.asmx";
        private const string LOOKUPTABLESWEBSERVICE = "_vti_bin/PSI/LookupTable.asmx";
        RBSViewer.CustomFieldsWsc.CustomFields CustomFields = new RBSViewer.CustomFieldsWsc.CustomFields();
        RBSViewer.LookupTablesWsc.LookupTable LookupTable = new RBSViewer.LookupTablesWsc.LookupTable(); 
        public ArrayList selecteditems;
        public Boolean multiselect;

        public LookupTablePicker()
        {
            InitializeComponent();
            multiselect = false;
        }

        public void PopulateControl()
        {
            treeView1.MultiSelect = multiselect;
            CustomFields.Url = MainForm.PWAURL + CUSTOMFIELDSSWEBSERVICE;
            CustomFields.Credentials = CredentialCache.DefaultCredentials;

            LookupTable.Url = MainForm.PWAURL + LOOKUPTABLESWEBSERVICE;
            LookupTable.Credentials = CredentialCache.DefaultCredentials;

            Guid[] LTList = new Guid[1];
            LTList[0] = LookupTableGuid;
            //Ooh naughty english only hack
            LookupTablesWsc.LookupTableDataSet lds = LookupTable.ReadLookupTablesByUids(LTList, false, 1033);
            foreach (DataRow temprow in lds.Tables["LookupTableTrees"].Rows)
            {
                if (temprow["LT_PARENT_STRUCT_UID"].ToString() == "")
                {
                    //root node
                    treeView1.Nodes.Add(temprow["LT_STRUCT_UID"].ToString(), temprow["LT_VALUE_TEXT"].ToString());
                }
                else
                {
                    //child
                   int index = treeView1.Nodes.IndexOfKey(temprow["LT_PARENT_STRUCT_UID"].ToString());
                    if(index == -1)
                    {
                        foreach (TreeNode treenode in treeView1.Nodes)
                            findandaddnode(treenode, temprow["LT_PARENT_STRUCT_UID"].ToString(), temprow["LT_STRUCT_UID"].ToString(),  temprow["LT_VALUE_TEXT"].ToString());
                    }
                    else
                    {
                        treeView1.Nodes[index].Nodes.Add(temprow["LT_STRUCT_UID"].ToString(), temprow["LT_VALUE_TEXT"].ToString());
                    }
                }
            }
            treeView1.ExpandAll();
        }
        void findandaddnode(TreeNode tn,String key, String StructID, String ValueText)
        {
            int index = tn.Nodes.IndexOfKey(key);
            if (index == -1)
            {
                foreach (TreeNode treenode in tn.Nodes)
                {
                    findandaddnode(treenode, key, StructID, ValueText);
                }
            }
            else
            {
                tn.Nodes[index].Nodes.Add(StructID, ValueText);
            }
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            selecteditems = treeView1.SelectedNodes;
            this.Hide();
        }

        private void Cancelbutton_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (selecteditems != null)
            {
                selecteditems.Clear();
            }
            this.Hide();
        }
    }
}